package com.marvels.system.common.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.dto.UserRechargeLogDTO;
import com.marvels.common.dto.pay.MarvelsPayResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.system.common.dao.SysMenuDao;
import com.marvels.system.common.dto.McsmRevealConfigureAppDTO;
import com.marvels.system.common.dto.SysMenuDTO;
import com.marvels.system.common.service.SysConfigService;
import com.marvels.system.common.service.SysLoginService;
import com.marvels.system.common.service.SysMenuService;
import com.marvels.system.common.service.UserRechargeLogService;
import com.marvels.system.permission.dto.SysRoleDTO;
import com.marvels.system.permission.dto.SysUserDTO;

/**
 * 系统服务
 * @author wangliang
 *
 */

@RestController
@RequestMapping("/system/service")
public class SystemServiceController {
	@Autowired
	private SysMenuService sysMenuService;
	
	@Autowired
	private SysLoginService sysLoginService;
	@Autowired
	SysMenuDao sysMenuDao;
	@Autowired
	private UserRechargeLogService UserRechargeLogService;
	@Autowired
	private SysConfigService sysConfigService;
	
	/**
	 * 获取登录用户的角色权限菜单
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/roleMenuTree")
	public MarvelsResultDTO getSysRoleMenuTree(){
		List<SysMenuDTO> list = null;
		try {
			list = sysMenuService.getSysRoleMenuTree();
		} catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getSysRoleMenuTree]",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), list);
	}
	
	/**
	 * 获取系统菜单树和角色权限菜单
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/menuTree")
	public MarvelsResultDTO getSysMenuTree(@RequestBody(required=false) SysRoleDTO role){
		Map<String, List> map = null;
		try {
			map = sysMenuService.getSysMenuTree(role);
		} catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getSysMenuTree]",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), map);
	}
	
	/**
	 * 系统登录验证
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/login")
	public MarvelsResultDTO login(@RequestBody UserDTO user,HttpServletRequest request){
		MarvelsResultDTO result = null;
		try {
			result = sysLoginService.login(user,request);
		} catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[login]",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/logout")
	public MarvelsResultDTO logout(){
		try {
			sysLoginService.logout();
		} catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[logout]",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null); 
	}
	
	@ResponseBody
	@RequestMapping("/editPassword")
	public MarvelsResultDTO editPassword(@RequestBody Map<String, String> requestMap){
		try {
			sysLoginService.editPassword(requestMap);
		} catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e);
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[editPassword]",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null); 
	}
	
	/**
	 * 获取登录用户的角色权限菜单MenuPath
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/currentUserMenuPath")
	public MarvelsResultDTO getCurrentUserMenuPath(){
		try {
			List<String> userMenuPath = sysMenuService.getUserMenuPath();
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), userMenuPath);
		} catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getSysRoleMenuTree]",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
	}
	
	@RequestMapping("freshAuthCash")
	public void freshAuthCash(){
		MarvelsLogUtil.getInstance().info("开始初始化权限信息");
		List<SysMenuDTO> menuList = sysMenuDao.selectAllPermission();
		Map<String,String> needPermissionMap = new HashedMap();
		for(SysMenuDTO sysMenuDTO : menuList) {
			needPermissionMap.put(sysMenuDTO.getMenuUrl(), sysMenuDTO.getMenuUrl());
			MarvelsLogUtil.getInstance().info("初始化权限信息"+sysMenuDTO.getMenuUrl());
		}
		MarvelsLogUtil.getInstance().info("结束初始化权限信息");
		CacheUtils.cacheObject("needPermissionMap", needPermissionMap);
	}
	

	@ResponseBody
	@RequestMapping("/initPassword")
	public MarvelsResultDTO initPassword(@RequestBody SysUserDTO user){
		try {
			sysLoginService.initPassword(user);
		} catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e);
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[editPassword]",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null); 
	}
	
	
	/**
	 * app前端展示配置
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/appRevealConfigure")
	public MarvelsPayResultDTO appRevealConfigure(@RequestBody(required=false) McsmRevealConfigureAppDTO appDTO){
		try {
			 Map<String,Object> map = sysConfigService.appRevealConfigure(appDTO);
			return MarvelsPayResultDTO.getSuccessResult(map);
		}catch(MarvelsBusinessException e){
			MarvelsLogUtil.getInstance().error(">>>>>>>>>app前端展示配置查询失败", e);
			return MarvelsPayResultDTO.getResult("-1", "app前端展示配置查询失败", null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>app前端展示配置查询异常", e);
			return MarvelsPayResultDTO.getResult("-1", "app前端展示配置查询异常", null);
		}
	}
	
	/**
	 * 查询配置列表
	 * @param configDto
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryConfigList")
	public MarvelsResultDTO queryConfigList(@RequestBody McsmRevealConfigureAppDTO configDto){
		try {
			List<McsmRevealConfigureAppDTO> list = sysConfigService.queryConfigList(configDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), list); 
		} catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e);
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[queryConfigList error]",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
	}
	
	/**
	 * 查询渠道列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryPayChannelList")
	public MarvelsResultDTO queryPayChannelList(){
		try {
			List<McsmRevealConfigureAppDTO> list = sysConfigService.queryPayChannelList();
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), list); 
		} catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e);
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[queryPayChannelList error]",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
	}
	
	/**
	 * 修改配置项
	 * @param configDto
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateConfigByKey")
	public MarvelsResultDTO updateConfigByKey(@RequestBody McsmRevealConfigureAppDTO configDto){
		try {
			McsmRevealConfigureAppDTO result = sysConfigService.updateConfigByKey(configDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), result); 
		} catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e);
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[queryConfigList error]",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
	}
	
	
	/**
	 * 查询系统启停配置
	 * @param configDto
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/querySysUpDown")
	public MarvelsResultDTO querySysUpDown(){
		try {
			List<UserRechargeLogDTO> list = UserRechargeLogService.selectUserRechargeLogList();
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), list); 
		} catch(Exception e){
			MarvelsLogUtil.getInstance().error("[queryConfigList error]",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
	}
	
	/**
	 * 修改系统启停配置
	 * @param configDto
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateSysUpDown")
	public MarvelsResultDTO updateSysUpDown(@RequestBody UserRechargeLogDTO param){
		try {
			UserRechargeLogDTO result = UserRechargeLogService.updateUserRechargeLog(param);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), result); 
		} catch(Exception e){
			MarvelsLogUtil.getInstance().error("[queryConfigList error]",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
	}
}
